import { Component, OnInit } from '@angular/core';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
    templateUrl: './index.html',
    styles: [
        `
          .tab-head .ant-btn{
            margin-right: 12px;
          }
          .tab-head .ant-input-search-button{
              margin:0;
          }
          .xq-item{
            margin-bottom: 20px;
            font-size:13px;
          }
          .xq-item span{
            color:#999;
          }
          
        `
    ],

})
export class OrdersComponent implements OnInit {
    page = {
        pageNo: 1,
        pages: 10,
        travelOrderId: "",
        loginId: "",
        beginTime: '',
        endTime: '',
    };
    dateRange;
    dataList = [];
    isVisible = false;
    isOkLoading = false;
    tableLoading = false;
    modalTitle;
    selectedRowData: any = {};
	validateForm;
	prepareMemo = '';
    isQX = false;
    isVisible2 = false;
    orderStatus = {};
    constructor(
        private apiService: ApiService,
        private message: MessageService,
        private fb: FormBuilder,
        private date: DatePipe,
        private nzMessageService: NzMessageService,
        private i18n:I18nService
    ) {
        this.i18n.get(['orderStatus','pagetitle']).subscribe(res=>{
            this.orderStatus = res.orderStatus;
            const pagetitle = res.pagetitle;
            this.message.setBreadcrumb({
                breadcrumbs: [{ name: pagetitle.title5}],
            });
        })
    }
    ngOnInit() {
        this.modalTitle = '';
        this.validateForm = this.fb.group({
            rejectMemo: ['', [Validators.required]]
        });
        this.loadList();
    }
    loadList() {
        this.tableLoading = true;
        this.apiService.post('预定订单列表', { ...this.page }).then((res: any) => {
            this.dataList = res.data.list;

            this.page.pageNo = res.data.pageNo;
            this.page.pages = res.data.pages * 10;
            this.tableLoading = false;
        });
    };
    changeCurrentPage(e) {
        if (e) {
            this.page.pageNo = e;
            this.loadList();
        }
    }
    selRow;
    isDel: boolean;
    handleCancel() {
        this.isVisible = this.isQX = false;
    };
    handleOk() {
        const data = { ...this.validateForm.getRawValue() };
        data['travelOrderId'] = this.selRow.id;
        data['rejectMemo'] ? this.ajx('取消订单', data) : '';
    };
    rejectMemo: string;
    del;
    onChangeStatus(data, s) {
        let url;
        this.selRow = data;
        switch (s) {
            case 'PENDING':
                this.isQX = true;
                break;
            case 'RECEIVED':
				if (!this.prepareMemo.trim()) {
					this.nzMessageService.warning('请填写行程确认内容！');
					return;
				}
                this.ajx('确认行程', { travelOrderId: data.id , prepareMemo: this.prepareMemo});
                break;
            default:
                this.isVisible = true;
                this.apiService.post('预约订单详情', { travelOrderId: data.id }).then((res: any) => {
                    this.del = res.data;
                });
                break;
        }
    };
    onChangeDate(e) {
        this.page['beginTime'] = e[0] ? this.date.transform(e[0], 'yyyy-MM-dd') : '';
        this.page['endTime'] = e[1] ? this.date.transform(e[1], 'yyyy-MM-dd') : '';
	};
	sure(data){
		this.isVisible2 = true;
		this.apiService.post('预约订单详情', { travelOrderId: data.id }).then((res: any) => {
			this.del = res.data;
		});
	}
	cancel(){
		this.isVisible2 = false;
		this.prepareMemo = '';
	}
    sherch() {
        this.loadList();
    };
	receiveTravel(data){
		this.apiService.post('接单', { travelOrderId: data.id }).then((res: any) => {
			this.handleCancel();
            this.loadList();
		});
	}
    ajx(url, data) {
        this.apiService.post(url, data).then((res: any) => {
			this.handleCancel();
			this.cancel();
            this.loadList();
        });
    }
}
